package com.tron.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.HashMap;

@RestController
public class ConfigController {

    RestTemplate restTemplate = new RestTemplate();

    @Autowired
    LoadBalancerClient loadBalancerClient;

    @Value("${spring.application.name}")
    String serverName;

    @GetMapping("/refresh")
    public Object refreshConfig() {
        ServiceInstance serviceInstance = loadBalancerClient.choose(serverName);
        URI uri = serviceInstance.getUri();
        restTemplate.postForObject(uri + "/actuator/bus-refresh", new HashMap<>(), Object.class);
        return "SUCCESS";
    }
}
